package xx.services.demo.rest;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.demo.DemoNavigation;
import xx.services.Passport;
import xx.services.demo.inter.INavigationInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * Demo导航模板服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/demo/navigation")
 * @author Administrator
 * 
 * 2010-12-20
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/demo/navigation")
public class NavigationRest extends Passport implements INavigationInterface
{

    public NavigationRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 新增一个导航demo。
     * 
     * @POST
     * 
     * @param title-@FormParam("title")
     *            导航标题
     * @param container-@FormParam("container")
     *            容器，一般是一个div，如：<div class="container">${container}</div>
     * @param templet-@FormParam("templet")
     *            模板
     * @param demo-@FormParam("demo")
     *            dome样例
     * @param shape-@FormParam("shape")
     *            样式图片
     * @param description-@FormParam("description")
     *            描述
     * @return 新导航实体
     * @author chenxiang
     */
    @POST
    public String add(@FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("templet")
    String templet, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoNavigation dn = new DemoNavigation(qr, owner);
            dn.setTitle(title).setContainer(container).setTemplet(templet);
            if (demo != null && !"".equals(demo.trim()))
            {
                dn.setDemo(demo);
            }
            if (shape != null && !"".equals(shape.trim()))
            {
                dn.setShape(shape);
            }
            if (description != null && !"".equals(description.trim()))
            {
                dn.setDescription(description);
            }
            dn.insert();
            return ok(dn);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 删除导航demo。
     * 
     * @DELETE
     * @Path ("/{navigation_id}")
     * 
     * @param navigationId-@PathParam("navigation_id")
     *            导航ID，多个ID用“_”隔开
     * @return true-删除成功，string-其他删除失败（string就是失败原因）
     * @author chenxiang
     */
    @DELETE
    @Path("/{navigation_id}")
    public String delete(@PathParam("navigation_id")
    String navigationId)
    {
        try
        {
            long[] navigationIdArr = Arr.toLongArray(navigationId, "_");
            int count = 0;
            for (long id : navigationIdArr)
            {
                DemoNavigation dn = new DemoNavigation(qr, owner);
                dn.setNavigationId(id);
                dn.delete();
                count++;
            }
            return ok(count);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取一个导航demo。
     * 
     * @GET
     * @Path ("/{navigation_id}")
     * 
     * @param navigationId-@PathParam("navigation_id")
     *            导航ID
     * @return 导航实体
     * @author chenxiang
     */
    @GET
    @Path("/{navigation_id}")
    public String get(@PathParam("navigation_id")
    long navigationId)
    {
        try
        {
            DemoNavigation dn = new DemoNavigation(qr, owner);
            dn.setNavigationId(navigationId);
            dn.load();
            return ok(dn);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 获取导航列表。
     * 
     * @GET
     * @return 导航列表
     * @author chenxiang
     */
    @GET
    public String gets()
    {
        try
        {
            DemoNavigation dn = new DemoNavigation(qr, owner);
            RowSet rs = dn.getList(pageSize, pageNo, condition, orderField, fields);
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                map.put(Constants.TOTAL, dn.getNavigationCount(condition));
            }
            return ok(dn.rsToList(rs), map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return error(e);
        }
    }

    /**
     * 更新指定的导航信息
     * 
     * @PUT
     * @Path ("/{navigation_id}")
     * 
     * @param navigationId-@PathParam("navigation_id")
     *            导航ID
     * @param title-@FormParam("title")
     *            导航标题
     * @param container-@FormParam("container")
     *            容器，一般是一个div，如：<div class="container">${container}</div>
     * @param templet-@FormParam("templet")
     *            模板
     * @param demo-@FormParam("demo")
     *            dome样例
     * @param shape-@FormParam("shape")
     *            样式图片
     * @param description-@FormParam("description")
     *            描述
     * @return 新导航实体
     * @author chenxiang
     */
    @PUT
    @Path("/{navigation_id}")
    public String update(@PathParam("navigation_id")
    long navigationId, @FormParam("title")
    String title, @FormParam("container")
    String container, @FormParam("templet")
    String templet, @FormParam("demo")
    String demo, @FormParam("shape")
    String shape, @FormParam("description")
    String description)
    {
        try
        {
            DemoNavigation dn = new DemoNavigation(qr, owner);
            dn.setNavigationId(navigationId);
            StringBuilder updateColumn = new StringBuilder();
            if (title != null && !"".equals(title.trim()))
            {
                dn.setTitle(title);
                updateColumn.append("," + DemoNavigation.TITLE);
            }
            if (container != null && !"".equals(container.trim()))
            {
                dn.setContainer(container);
                updateColumn.append("," + DemoNavigation.CONTAINER);
            }
            if (templet != null && !"".equals(templet.trim()))
            {
                dn.setTemplet(templet);
                updateColumn.append("," + DemoNavigation.TEMPLET);
            }
            if (demo != null && !"".equals(demo.trim()))
            {
                dn.setDemo(demo);
                updateColumn.append("," + DemoNavigation.DEMO);
            }
            if (shape != null && !"".equals(shape.trim()))
            {
                dn.setShape(shape);
                updateColumn.append("," + DemoNavigation.SHAPE);
            }
            if (description != null && !"".equals(description.trim()))
            {
                dn.setDescription(description);
                updateColumn.append("," + DemoNavigation.DESCRIPTION);
            }
            if (updateColumn.length() > 0)
            {
                dn.update(updateColumn.toString());
                return ok(dn);
            }
            else
            {
                return error("更新内容不能全部为空");
            }
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
